package com.yanqu.road.dao.impl.curio;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.curio.UserCurioSkillData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserCurioSkillDataDaoImpl extends TempDataObjectDao<UserCurioSkillData> {

    public long getUserCurioSkillAddition(long userId, List<Integer> skillIdList){
        ResultSet rs = null;
        CallableStatement call = null;
        long addition = 0;
        if(skillIdList.size() == 0){
            return addition;
        }
        String sql = "select sum(addition) as addition from t_u_curio_skill where userId = ? and skill in("+ StringUtils.listToString(skillIdList, ",") +")";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                addition = rs.getLong("addition");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return addition;
    }

    public static String getUserCurioSkillDataSql = "select * from `t_u_curio_skill` where `userId`=?";
    public Map<Integer, UserCurioSkillData> getUserCurioSkillData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UserCurioSkillData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserCurioSkillDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserCurioSkillData data = parse(rs);
                map.put(data.getSkill(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateUserCurioSkillDataSql = "update `t_u_curio_skill` set skillType=?,objId=?,lv=?,addition=? where userId=? and skill=?";
    @Override
    protected boolean updateData(UserCurioSkillData userCurioSkillData) {
        userCurioSkillData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userCurioSkillData.getSkillType()));
        para.put(i++, new DbParameter(Types.INTEGER, userCurioSkillData.getObjId()));
        para.put(i++, new DbParameter(Types.INTEGER, userCurioSkillData.getLv()));
        para.put(i++, new DbParameter(Types.BIGINT, userCurioSkillData.getAddition()));
        para.put(i++, new DbParameter(Types.BIGINT, userCurioSkillData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userCurioSkillData.getSkill()));
        if (getDb().executeNoneQuery(updateUserCurioSkillDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserCurioSkillDataSql = "insert into `t_u_curio_skill`(userId,skill,skillType,objId,lv,addition) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserCurioSkillData userCurioSkillData) {
        userCurioSkillData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userCurioSkillData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userCurioSkillData.getSkill()));
        para.put(i++, new DbParameter(Types.INTEGER, userCurioSkillData.getSkillType()));
        para.put(i++, new DbParameter(Types.INTEGER, userCurioSkillData.getObjId()));
        para.put(i++, new DbParameter(Types.INTEGER, userCurioSkillData.getLv()));
        para.put(i++, new DbParameter(Types.BIGINT, userCurioSkillData.getAddition()));
        if (getDb().executeNoneQuery(insertUserCurioSkillDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserCurioSkillData parseData(ResultSet rs) throws SQLException {
        UserCurioSkillData userCurioSkillData = new UserCurioSkillData();
        userCurioSkillData.setUserId(rs.getLong("userId"));
        userCurioSkillData.setSkill(rs.getInt("skill"));
        userCurioSkillData.setSkillType(rs.getInt("skillType"));
        userCurioSkillData.setObjId(rs.getInt("objId"));
        userCurioSkillData.setLv(rs.getInt("lv"));
        userCurioSkillData.setAddition(rs.getLong("addition"));

        return userCurioSkillData;
    }
}
